package com.cpi.cornpp.purchase.program.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseOrderAssignFutureForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderAssignFutureView;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderAssignFuturesView;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderView;
import com.cpi.cornpp.purchaseorder.spot.exception.PurchaseOrderSpotException;
import com.cpi.cornpp.purchaseorder.spot.services.PurchaseOrderSpotService;
import com.cpi.cornpp.purchaseorder.spot.view.PurchaseOrderSpotView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;
import com.cpi.cornpp.unit.service.UnitService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;

public class PurchaseProgramAssignAction extends Action {
	/** Success forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Id de la orden de compra */
	private final static String PURCHASE_ORDER_ID_LABEL = "purchaseOrderId";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Atributo donde se guardara  la vista de orden de compra en request */
	private static final String PURCHASE_ORDER_VIEW_ON_REQUEST = "purchaseOrderView";//$NON-NLS-1$
	/** Atributo donde se guardara  la vista de orden de compra en request */
	private static final String TRANSPORT_VIEW_ON_REQUEST = "transportView";//$NON-NLS-1$
	/** Atributo donde se guardaran las vistas de futuros */
	private static final String FUTURES_VIEWS_ON_REQUEST = "futuresPurchaseView";//$NON-NLS-1$
	private static final String SPOT_VIEWS_ON_REQUEST = "purchaseOrderSpotViews";//$NON-NLS-1$
	private static final String RECORDS_TO_SPOTS = "RECORDS_TO_SPOTS";//$NON-NLS-1$
	private static final String BUSHEL_UNIT = "BUSHEL_UNIT";//$NON-NLS-1$
	private static final String CON_UNIT = "CON_UNIT";
	private SystemParameterCollection systemParameterCollection = null;
	private SystemParameterBean systemParameterBean = null;
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException, PurchaseOrderSpotException, TransportException, FuturePurchaseException {
		HttpSession httpSession = null;
		PurchaseOrderView purchaseOrderView = null;
		TransportView transportView = null;
		TransportService transportService = null;
		PurchaseProgramService purchaseProgramService = null;
		FuturePurchaseService futurePurchaseService = null;
		PurchaseOrderSpotService purchaseOrderSpotService = null;
		UnitService unitService = null;
		int purchaseOrderId = -1;
		List futurePurchaseViews = null;
		List purchaseOrderSpotViews = null;
		PurchaseOrderAssignFutureForm purchaseOrderAssignFutureForm = null;
		PurchaseOrderAssignFuturesView purchaseOrderAssignFuturesView = null;
		try{
			purchaseProgramService = new PurchaseProgramService();
			futurePurchaseService = new FuturePurchaseService();
			transportService = new TransportService();
			unitService = new UnitService();
			purchaseOrderSpotService = new PurchaseOrderSpotService();
			purchaseOrderAssignFutureForm = (PurchaseOrderAssignFutureForm)form;
			purchaseOrderAssignFuturesView = purchaseOrderAssignFutureForm.getPurchaseOrderAssignFuturesView();
			httpSession = request.getSession();			
			if(request.getParameter(PURCHASE_ORDER_ID_LABEL) != null){
				purchaseOrderId = NumberUtil.getInt(request.getParameter(PURCHASE_ORDER_ID_LABEL));
				purchaseOrderAssignFutureForm.getPurchaseOrderAssignFuturesView().setPurchaseOrderId(purchaseOrderId);
			}else{				
				purchaseOrderId = purchaseOrderAssignFutureForm.getPurchaseOrderAssignFuturesView().getPurchaseOrderId();			
			}
			purchaseOrderView = purchaseProgramService.getPurchaseOrderById(purchaseOrderId);
			transportView = transportService.getTransport(NumberUtil.getInt(purchaseOrderView.getPurchaseOrderTransportId()));
			futurePurchaseViews = futurePurchaseService.getFuturePurchasesByAvailable(purchaseOrderId,  purchaseOrderAssignFutureForm.getOrderField(),  (1 == purchaseOrderAssignFutureForm.getOrderType()) ? false : true);
			purchaseOrderSpotViews = purchaseOrderSpotService.getPurchaseOrderSpot(purchaseOrderId);
			request.setAttribute(PURCHASE_ORDER_VIEW_ON_REQUEST, purchaseOrderView);
			request.setAttribute(TRANSPORT_VIEW_ON_REQUEST, transportView);
			request.setAttribute(FUTURES_VIEWS_ON_REQUEST, futurePurchaseViews);
			
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(BUSHEL_UNIT);
			request.setAttribute(BUSHEL_UNIT, Double.toString(unitService.getUnitQuantity(systemParameterBean.getValue())));
			
			systemParameterBean = systemParameterCollection.getSystemParameterBean(CON_UNIT);
			request.setAttribute(CON_UNIT, Double.toString(unitService.getUnitQuantity(systemParameterBean.getValue())));

			
			purchaseOrderAssignFuturesView.setPurchaseOrderId(NumberUtil.getInt(purchaseOrderView.getPurchaseOrderId()));
			purchaseOrderAssignFuturesView.setPurchaseOrderAssignFutureViews(fillFuturesToAssign(futurePurchaseViews));
			purchaseOrderAssignFuturesView.setPurchaseOrderSpotViews(fillSpot(purchaseOrderSpotViews,purchaseOrderId));
			request.setAttribute(SPOT_VIEWS_ON_REQUEST, purchaseOrderAssignFuturesView.getPurchaseOrderSpotViews());
			purchaseOrderAssignFuturesView.setCommission(purchaseProgramService.getCostCommission(purchaseOrderId));
			httpSession.setAttribute("previousOrderField", Integer.toString(purchaseOrderAssignFutureForm.getOrderField()));
			httpSession.setAttribute("previousOrderType", Integer.toString(purchaseOrderAssignFutureForm.getOrderType()));
		}catch (PurchaseProgramException purchaseProgramException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}catch (PurchaseOrderSpotException purchaseOrderSpotException) {
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseOrderSpotException);
			throw purchaseOrderSpotException;
		}catch (TransportException transportException) {
			httpSession.setAttribute(CUSTOM_EXCEPTION, transportException);
			throw transportException;
		}catch (FuturePurchaseException futurePurchaseException) {
			httpSession.setAttribute(CUSTOM_EXCEPTION, futurePurchaseException);
			throw futurePurchaseException;
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}	
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	private List fillSpot(List purchaseOrderSpotViews, int purchaseOrderId) {
		List spot = null;
		spot = new ArrayList(0);
		int totalRecordsToAdd = 0;
		PurchaseOrderSpotView purchaseOrderSpotView = null;
		systemParameterCollection = SystemParameterCollection.getInstance();
		systemParameterBean = systemParameterCollection.getSystemParameterBean(RECORDS_TO_SPOTS);
		spot.addAll(purchaseOrderSpotViews);
		totalRecordsToAdd = systemParameterBean.getIntValue() - purchaseOrderSpotViews.size();
		for( int int_i = totalRecordsToAdd; int_i>0; int_i-- ){
			purchaseOrderSpotView = new PurchaseOrderSpotView();
			purchaseOrderSpotView.setPurchaseOrderId(purchaseOrderId);
			spot.add(purchaseOrderSpotView);
		}
		
		return spot;
	}

	private List fillFuturesToAssign(List futurePurchaseViews){
		List futuresToAssign = null;
		Iterator iterator = null;
		FuturePurchaseView futurePurchaseView = null;
		PurchaseOrderAssignFutureView purchaseOrderAssignFutureView = null;
		
		futuresToAssign = new ArrayList(0);
		iterator = futurePurchaseViews.iterator();
		while(iterator.hasNext()){
			futurePurchaseView = (FuturePurchaseView)iterator.next();
			purchaseOrderAssignFutureView = new PurchaseOrderAssignFutureView();
			purchaseOrderAssignFutureView.setCornFutureId(futurePurchaseView.getFuturePurchaseId());
			purchaseOrderAssignFutureView.setQuantity(futurePurchaseView.getQuantityAssgined());
			purchaseOrderAssignFutureView.setTotalQuantity(futurePurchaseView.getQuantity());
			purchaseOrderAssignFutureView.setAccountFuturePeriodName(futurePurchaseView.getAccountFuturePeriodName());
			purchaseOrderAssignFutureView.setApplicationFuturePeriodName(futurePurchaseView.getApplicationFuturePeriodName());
			purchaseOrderAssignFutureView.setReference(futurePurchaseView.getReference());
			purchaseOrderAssignFutureView.setAvailables(futurePurchaseView.getAvailables());
			purchaseOrderAssignFutureView.setAssigned(futurePurchaseView.isFutureAssigned());
			purchaseOrderAssignFutureView.setUnitPrice(NumberUtil.convertPrice(futurePurchaseView.getUnitPrice()));
			futuresToAssign.add(purchaseOrderAssignFutureView);
		}
		return futuresToAssign;
	}
}